# Maintainer: James Duley <jagduley gmail>
# Maintainer: Philippe Renon <philippe_renon yahoo.fr>

_realname=osgearth
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=3.2
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('LGPL')
url="http://osgearth.org"
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libzip"
         "${MINGW_PACKAGE_PREFIX}-poco"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-rocksdb"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
             "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug"
             "${MINGW_PACKAGE_PREFIX}-osgQt"
             "${MINGW_PACKAGE_PREFIX}-osgQt-debug")
options=('staticlibs' 'strip')
source=("https://github.com/gwaldron/osgearth/archive/${_realname}-${pkgver}.tar.gz")
sha256sums=('7e1dd643b1f3b8d1ba9561b899c18176af988342b86c42d89a70be924cb747f6')

build() {
  _pkgver=${_realname}-${pkgver} # How it's tagged in git

  export FREETYPE_DIR=${MINGW_PREFIX}

  for builddir in {Release,Debug}-${MSYSTEM}; do
    [[ -d ${builddir} ]] && rm -rf ${builddir}
    mkdir -p ${builddir} && pushd ${builddir}
      [[ "${builddir%-${MSYSTEM}}" == "Release" ]] && {
        local _opts=""
      } || {
        local _opts=""
      }

      msg "Building ${builddir%-${MSYSTEM}} version..."

      MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
        -G"Ninja" \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        -DCMAKE_BUILD_TYPE=${builddir%-${MSYSTEM}} \
        -DOSG_DIR=${MINGW_PREFIX} \
        -DOSGEARTH_BUILD_EXAMPLES=OFF \
        -DOSGEARTH_BUILD_TESTS=OFF \
        -DOSGEARTH_BUILD_LEGACY_SPLAT_NODEKIT=ON \
        -DOSGEARTH_BUILD_LEVELDB_CACHE=ON \
        -DOSGEARTH_BUILD_ZIP_PLUGIN=ON \
        ${_opts} \
        ../${_realname}-${_pkgver}

      cmake --build .
    popd
  done
}

package_osgearth() {
  depends+=("${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
            "${MINGW_PACKAGE_PREFIX}-osgQt")
  pkgdesc="A terrain rendering toolkit for OpenSceneGraph (mingw-w64)"

  cd Release-${MSYSTEM}
  DESTDIR=${pkgdir} cmake --install .

  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/osgearth
  cp -rf ${srcdir}/${_realname}-${_realname}-${pkgver}/data ${pkgdir}${MINGW_PREFIX}/share/osgearth
}

package_osgearth-debug() {
  depends+=("${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug"
            "${MINGW_PACKAGE_PREFIX}-osgQt-debug")
  options=('staticlibs' '!strip')
  pkgdesc="A terrain rendering toolkit for OpenSceneGraph (debug) (mingw-w64)"

  cd Debug-${MSYSTEM}
  DESTDIR=${pkgdir} cmake --install .

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm -rf ${pkgdir}${MINGW_PREFIX}/share
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
